package saucaca.fragment;

import java.util.ArrayList;

import org.json.JSONException;

import saucaca.Adapter.CommentAdapter;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.AlbumObject;
import saucaca.Object.CommentObject;
import saucaca.Object.HomeData;
import saucaca.Object.SongObject;
import saucaca.Util.CMD;
import saucaca.Util.CirclePageIndicator;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class AlbumDetailFargment extends Fragment {
	String TAG = AlbumDetailFargment.class.getSimpleName();
	MainActivity mActivity;
	AlbumObject albums;
	ArrayList<SongObject> listsongs;
	CommentAdapter adapter;
	ArrayList<CommentObject> listComment;
	AlbumObject albumObject;
	ViewPager viewPager;
	CirclePageIndicator mIndicator;
	OfflineTabPageAdapter TabAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.album_detail, container, false);
		findViewById(view);
		Bundle bundle = getArguments();
		if (bundle != null) {
			albumObject = (AlbumObject) bundle.getSerializable(MainActivity.ID);
		}
		if (albumObject != null) {
			AlbumDetaisContent albumDetaisContent = new AlbumDetaisContent(mActivity, albumObject, new ArrayList<SongObject>());
			CommentFragment frComment = CommentFragment.newInstance(albumObject, TagName.Comment_typeAlbum);
			TabAdapter = new OfflineTabPageAdapter(getChildFragmentManager(), albumDetaisContent, frComment);
			viewPager.setAdapter(TabAdapter);
			mIndicator.setViewPager(viewPager);
			new GetFromServices().execute(albumObject.getALBUM_ID());
		}
		mActivity.myActionBar.setMenu(albumObject.getTXNAME(), false, false);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(albumObject.getTXNAME(), false, false);
	}

	public void findViewById(View view) {
		viewPager = (ViewPager) view.findViewById(R.id.main_view_pager);
		mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
		mIndicator.setCentered(true);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			activity = (MainActivity) activity;
		}
	}

//	void showAlbumDetail() {
//		txtTitleMenu.setText("Album: "+albumObject.getTXNAME());
//		txtArtistMenu.setText(albumObject.getSEO_NAME());
//	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.idDetais(CMD.CMD_ALBUM_DETAIL, params[0]);
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if(result!=null)
			try {
				JsonReader.readHeader(result);
				if(HomeData.headerObject.getCODE().equals("0")){
					if (JsonReader.readAlbumdetail(result).size()>0) {
						albumObject=JsonReader.readAlbumdetail(result).get(0);
						listsongs = JsonReader.readAlbum_listSong(result);
						mActivity.myActionBar.setTitle(albumObject.getTXNAME());
						TabAdapter.frMain.update(albumObject, listsongs);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	public class OfflineTabPageAdapter extends FragmentStatePagerAdapter {
		public AlbumDetaisContent frMain = null;
		public CommentFragment frComment = null;

		public OfflineTabPageAdapter(FragmentManager fm, AlbumDetaisContent frMain, CommentFragment frComment) {
			super(fm);
			this.frMain = frMain;
			this.frComment = frComment;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return null;
		}

		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Fragment getItem(int i) {
			switch (i) {
			case 1:
				return frComment;
			default:
				return frMain;
			}
		}
	}
}
